﻿using Kingdee.BOS.Core.Bill.PlugIn;
using Kingdee.BOS.Core.Bill.PlugIn.Args;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RK.BillPlugIn.Ronma
{
    [Description("润马：设置样品、废品仓进出单据的库存状态为不参与核算"), HotUpdate]
    public class SetStockStatusId : AbstractBillPlugIn
    {
        public override void BeforeSave(BeforeSaveEventArgs e)
        {
            base.BeforeSave(e);
            var formId = this.Model.BusinessInfo.GetForm().Id;
            DetailEntity info = GetDetailInfo(formId);
            var obj = this.Model.DataObject;
            DynamicObjectCollection rows = obj[info.OrmId] as DynamicObjectCollection;
            for (int i = 0; i < rows.Count; i++)
            {
                var stock = rows[i][info.stockId] as DynamicObject;
                string stockName = stock["Name"].ToString();
                if (stockName.Contains("样品") || stockName.Contains("废品"))
                {
                    this.Model.SetItemValueByNumber("FStockStatusId", "KCZT001", i);
                }
            }
        }

        private DetailEntity GetDetailInfo(string formId)
        {
            DetailEntity detail = new DetailEntity();
            detail.stockId = "StockId";
            detail.StockStatusId_Id = "StockStatusId_Id";
            switch (formId)
            {
                //采购入库单
                case "STK_InStock":
                    detail.OrmId = "InStockEntry";
                    break;
                //采购退料单
                case "PUR_MRB":
                    detail.OrmId = "PUR_MRBENTRY";
                    break;
                //其他入库单
                case "STK_MISCELLANEOUS":
                    detail.OrmId = "STK_MISCELLANEOUSENTRY";
                    detail.stockId = "FSTOCKID";
                    break;
                //其他出库单
                case "STK_MisDelivery":
                    detail.OrmId = "BillEntry";
                    break;
                //受托加工材料入库单
                case "STK_OEMInStock":
                    detail.OrmId = "OEMInStockEntry";
                    break;
                //受托加工材料退料单
                case "STK_OEMInStockRETURN":
                    detail.OrmId = "OEMInStockRTNEntry";
                    break;
                //销售出库单
                case "SAL_OUTSTOCK":
                    detail.OrmId = "SAL_OUTSTOCKENTRY";
                    break;
                //销售退货单
                case "SAL_RETURNSTOCK":
                    detail.OrmId = "SAL_RETURNSTOCKENTRY";
                    break;
                //简单生产领料单
                case "SP_PickMtrl":
                    detail.OrmId = "Entity";
                    break;
                //简单生产退料单
                case "SP_ReturnMtrl":
                    detail.OrmId = "Entity";
                    break;
                //生产补料单
                case "PRD_FeedMtrl":
                    detail.OrmId = "Entity";
                    break;
                //生产领料单
                case "PRD_PickMtrl":
                    detail.OrmId = "Entity";
                    break;
                //生产退料单
                case "PRD_ReturnMtrl":
                    detail.OrmId = "Entity";
                    break;
                default:
                    detail.OrmId = "Entity";
                    detail.stockId = "StockId";
                    break;
            }
            return detail;
        }
    }

    internal class DetailEntity
    {
        /// <summary>
        /// 明细单据体Orm实体名
        /// </summary>
        public string OrmId { get; set; }
        /// <summary>
        /// 明细仓库orm实体名
        /// </summary>
        public string stockId { get; set; }
        /// <summary>
        /// 库存状态标识
        /// </summary>
        public string FStockStatusId { get; set; }
        public string StockStatusId_Id { get;set; }
    }
}
